package com.phy.gsjl.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.phy.gsjl.entity.Reply;

/**
 * reply实体类的数据库访问层
 * @author curry
 *
 */
@Repository
public class ReplyDAO {

	@Autowired
	private SessionFactory sessionFactory;
	
	//获取日志对象
	private static Logger logger = Logger.getLogger(ReplyDAO.class);
		
	private Session getSession() {
		return sessionFactory.getCurrentSession();
	}
	
	/**
	 * 根据信息id返回所有回复信息，按时间排序
	 * @param m_id
	 * @return 
	 */
	public List<Reply> getAllReplys(Integer m_id) {
		String hql = "from Reply where m_id = ? order by r_time";
		//获取session，生成query对象，设置参数，执行查询操作
		List<Reply> replys = this.getSession().createQuery(hql).setParameter(0, m_id).list();
		logger.info("==>In ReplyDAO getAllReplys hql: " + hql);
		return replys;
	}
	
	/**
	 * 添加回复信息
	 * @param reply
	 */
	public void addReply(Reply reply) {
		logger.info("==>In ReplyDAO addReply info of message: " + reply.toString());
		this.getSession().save(reply);
	}
}
